package edu.cueb.wmis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.cueb.wmis.mapper.*;
import edu.cueb.wmis.pojo.*;
import edu.cueb.wmis.service.IOutbillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.cueb.wmis.utils.AutoBillNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class OutbillServiceImpl extends ServiceImpl<OutbillMapper, Outbill> implements IOutbillService {

    @Autowired
    private OrderbillMapper orderbillMapper;
    @Autowired
    private OrderbillitemMapper orderbillitemMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private OutbillMapper outbillMapper;
    @Autowired
    private EmptyMapper emptyMapper;
    @Override
    public boolean generateOutbill(Outbill outbill) {
        //自动生成单号：20xx年第02号
       // String billName = AutoBillNameUtils.generateName(out)
        String year = ((Integer)LocalDate.now().getYear()).toString();
        Integer billCount = outbillMapper.findMaxBillName(year);
        String billName = AutoBillNameUtils.getBillName(year,billCount);
        Integer orderId = outbill.getOrderId();
        orderbillMapper.updateState(orderId);
        List<Orderbillitem> orderbillitemList = orderbillitemMapper.selectList(new QueryWrapper<Orderbillitem>().eq("billId",orderId));

        BigDecimal outAmount = new BigDecimal(0);
        for(Orderbillitem item : orderbillitemList){
            outAmount = outAmount.add(item.getAmount());
            goodMapper.reduceStoreNumber(item.getGoodId(),item.getNumber()); //减库存
            List<Integer> nilShelfIds =  goodMapper.getEmptyShelf();
//            List<Good> nilGoods = goodMapper.selectList(new QueryWrapper<Good>().eq("storeNumber", 0));
//            if(nilGoods.size() != 0){
//                Empty
//                emptyMapper.
//            }
            if(nilShelfIds.size() != 0){
                for(Integer nilShelfId : nilShelfIds){
                    Empty empty = new Empty();
                    empty.setNilShelfId(nilShelfId);
                    emptyMapper.insert(empty);
                }
            }
        }
        outbill.setOutAmount(outAmount);
        outbill.setOutTime(LocalDate.now());
        outbill.setBillName(billName);
        return outbillMapper.insert(outbill)==1 ? true : false;
    }

    @Override
    public List<Outbill> selectOutbill() {
        return outbillMapper.selectList(new QueryWrapper<Outbill>());

    }

    @Override
    public Outbill selectItemByBillId(Integer id) {
        Outbill outbill = outbillMapper.selectById(id);
        Orderbill orderbill = orderbillMapper.selectById(outbill.getOrderId());
        List<Orderbillitem> orderbillitems = orderbillitemMapper.selectList(new QueryWrapper<Orderbillitem>().eq("billId",orderbill.getId()));
        outbill.setOrderbill(orderbill);
        outbill.setOrderbillitems(orderbillitems);
        return outbill;
    }
}
